package com.cet.algorithm.贪心算法.最大子序列和;

/**
 * @program: cet-practice
 * @description: 最大子序列和
 * @author: 陈恩涛
 * @create: 2023-06-06 22:22
 **/
public class LC53 {

    public int maxSubArray(int[] nums) {

        // 连续和为负数，当前的连续序列就是累赘
        int sum = 0;

        // 用于保存负数
        int result = Integer.MIN_VALUE;

        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            result = Math.max(result, sum);
            if (sum < 0) {
                sum = 0;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        LC53 lc53 = new LC53();
        System.out.println(lc53.maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4}));
        System.out.println(lc53.maxSubArray(new int[]{-1, -1, -1, -1}));
    }
}
